package com.ruoyi.back.controller;

import com.ruoyi.back.domain.Chunk;
import com.ruoyi.back.domain.Fileinfo;
import com.ruoyi.back.service.IChunkService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UpLoadUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.uuid.SnowflakeIdUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @ClassName UploadController
 * @Description TODO 大文件上传
 * @Author luguanqing
 * @Date 2021/4/9 16:15
 * @Version 1.0
 */
@Api(value="大文件上传",description = "大文件上传接口")
@RestController
@RequestMapping("/back/uploader")
public class UploadController {

    @Autowired
    private IChunkService chunkService;

    @PostMapping("/chunk")
    public AjaxResult uploadChunk(Chunk chunk){
        MultipartFile file = chunk.getFile();
        try{
            //文件上传到指定路径
            String fileName = FileUploadUtils.uploadBigVideo(RuoYiConfig.getUploadPath() + "/" +StringUtils.substring(file.getOriginalFilename(),0, StringUtils.lastIndexOf(file.getOriginalFilename(), ".")) , file, MimeTypeUtils.MEDIA_EXTENSION);
            //新文件名称
            String newFilePath = StringUtils.substring(fileName, 0, StringUtils.lastIndexOf(fileName, ".")) + "-" + chunk.getChunkNumber() + StringUtils.substring(fileName, StringUtils.lastIndexOf(fileName, "."), fileName.length());
            String oldFilePath = StringUtils.replace(fileName, "/profile", RuoYiConfig.getProfile());
            File files = new File(oldFilePath);
            newFilePath = StringUtils.replace(newFilePath, "/profile", RuoYiConfig.getProfile());
            files.renameTo(new File(newFilePath));
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            return ajax;
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error("上传文件异常，请联系管理员");
    }

//    @GetMapping("/chunk")
//    public AjaxResult checkChunk(Chunk chunk) {
//        AjaxResult ajax;
//        if (chunkService.checkChunk(chunk.getIdentifier(), chunk.getChunkNumber())) {
//            ajax = AjaxResult.success();
//            ajax.put("flag", true);
//            return ajax;
//        }else{
//            ajax = AjaxResult.error();
//            ajax.put("flag", false);
//        }
//        return ajax;
//    }

    @PostMapping("/mergeFile")
    public AjaxResult mergeFile(@RequestParam("filename") String filename) {
        String path = RuoYiConfig.getUploadPath() + "/" + filename + ".mp4";
        String folder = RuoYiConfig.getUploadPath()+ "/" + filename;
        UpLoadUtils.merge(path, folder);
//        fileInfo.setLocation(path);
        return AjaxResult.success("合并成功");
    }

}
